﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class Player
{

   private static int wAmount;
   private static int pAmount;
   private static int aAmount;
   private static int bAmount;
   private static int kAmount;
   private string pname;
    public Player(string pname)
    {
        this.pname = pname;
        wAmount = 0;
        pAmount = 0;
        aAmount = 0;
        bAmount = 0;
        kAmount = 0;

    }


    private static int Buy(int amount, string type, int remainingMoney)
    {
        int remainder = 0;
        switch (type.ToLower())
        {
            case "warrior": if (remainingMoney - amount * 50 >= 0)
                {
                    remainder = remainingMoney - amount * 50; wAmount += amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("Not enough gold, press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }

            case "barbarian": if (remainingMoney - amount * 80 >= 0)
                {
                    remainder = remainingMoney - amount * 80; bAmount += amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("Not enough gold, press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            case "archer": if (remainingMoney - amount * 75 >= 0)
                {
                    remainder = remainingMoney - amount * 75; aAmount += amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("Not enough gold, press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            case "knight": if (remainingMoney - amount * 120 >= 0)
                {
                    remainder = remainingMoney - amount * 120; kAmount += amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("Not enough gold, press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            case "pikeman": if (remainingMoney - amount * 65 >= 0)
                {
                    remainder = remainingMoney - amount * 65; pAmount += amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("Not enough gold, press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            default: return remainingMoney;
        }
    }

    //trqbva da se slojat proverki pri Sell, dali ne se prodava poveche, ot kolkoto ima!!!
    private static int Sell(int amount, string type, int remainingMoney)
    {
        int remainder = 0;
        switch (type.ToLower())
        {
            case "warrior": if (wAmount>=amount)
                {
                    remainder = remainingMoney + amount * 25; wAmount -= amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("You dont own This many warriors. Press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            case "barbarian": if (bAmount >= amount)
                {
                    remainder = remainingMoney + amount * 40; bAmount -= amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("You dont own This many barbarians. Press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            case "archer": if (aAmount >= amount)
                {
                    remainder = remainingMoney + amount * 37; aAmount -= amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("You dont own This many archers. Press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            case "knight": if (kAmount >= amount)
                {
                    remainder = remainingMoney + amount * 60; kAmount -= amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("You dont own This many knights. Press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            case "pikeman": if (pAmount >= amount)
                {
                    remainder = remainingMoney + amount * 32; pAmount -= amount;
                    return remainder;
                }
                else
                {
                    Console.WriteLine("You dont own This many pikemen. Press any key to try again.");
                    Console.ReadLine();
                    return remainingMoney;
                }
            default: return remainingMoney;
        }
    }

    public void Market(int availableGold)
    {
        
        while (availableGold >= 0)
        {
            Console.WriteLine("{0} is shopping at the moment.",pname);
            Console.WriteLine("You have {0}  remaining gold", availableGold);
            Console.WriteLine("1 Warrior costs 50 gold. You own {0}\n1 Barbarian cost 80 gold each. You own {1}\n1 Archer cost 75 gold. You own {2}\n1 Knight cost 120 gold. You own {3}", wAmount, bAmount, aAmount, kAmount);
            Console.WriteLine("1 Pikeman cost 65 gold. You own {0}\n\nEnter your choice like this: buy/sell <class> amount. For example - buy warrior 20\nEnter \"done\" finish shopping", pAmount);
           
            string input = Console.ReadLine();

            if (input == string.Empty)
            {
                Console.Clear();
                Market(availableGold);
                    return;
            } 
            
            input = input.ToLower();
            if (input.ToLower() == "done")
            {
                break;
            }
            else
            {
                string[] choice = input.Split(' ');
                try
                {
                    int.Parse(choice[2]);
                }
                catch (FormatException ex)
                {
                    Console.Clear();
                    Console.WriteLine(ex.Message);
                    Console.WriteLine();
                    Market(availableGold);

                }
                switch (choice[0].ToLower())
                {
                    case "buy": availableGold = Buy(int.Parse(choice[2]), choice[1], availableGold); break;
                    case "sell": availableGold = Sell(int.Parse(choice[2]), choice[1], availableGold); break;
                    default:
                        break;
                }
            }
            
            Console.Clear();
        }
    }

    public int WAmount
    {
        get
        {
            return wAmount;
        }
    }
    public int PAmount
    {
        get
        {
            return pAmount;
        }
    }
    public int AAmount
    {
        get
        {
            return aAmount;
        }
    }
    public int BAmount
    {
        get
        {
            return bAmount;
        }
    }
    public int KAmount
    {
        get
        {
            return kAmount;
        }
    }
    public string PName
    {
        get
        {
            return pname;
        }
    }
}

